#ifndef LOGINWINDOW_H
#define LOGINWINDOW_H

#include <QDialog>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>

#include "register_dialog.hpp"

// Forward declaration if your C client functions are wrapped
// extern "C" {
// int perform_login(const char* username, const char* password);
// }

// 前向声明ClientWrapper类
class ClientWrapper;

class LoginWindow : public QDialog
{
    Q_OBJECT

public:
    explicit LoginWindow(QWidget *parent = nullptr);
    explicit LoginWindow(ClientWrapper *clientWrapper, QWidget *parent = nullptr);
    ~LoginWindow();

    QString getLastSuccessfulUsername() const; // Getter for username

signals:
    void loginSuccessful(const QString &username); // Emit username on successful login
    void loginCancelled();

private slots:
    void handleLoginAttempt();
    void handleCancel();
    void onLoginResult(bool success, const QString &messageOrUser); // New slot
    void handleRegisterClicked();                                   // Added slot for register button

private:
    QLabel *m_usernameLabel;
    QLineEdit *m_usernameLineEdit;
    QLabel *m_passwordLabel;
    QLineEdit *m_passwordLineEdit;
    QPushButton *m_loginButton;
    QPushButton *m_cancelButton;
    QPushButton *m_registerButton; // Added register button member

    // ClientWrapper指针，用于与客户端API交互
    ClientWrapper *m_clientWrapper;
    QString m_lastSuccessfulUsername; // To store the username upon successful login

    void setupUi();
};

#endif // LOGINWINDOW_H